﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.DAL;
using OgilvyOne.UKNow.Mobile.Model.Events;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.UKNow.Mobile.Utilities;

namespace OgilvyOne.UKNow.Mobile.APIs.Events
{
  public  class CheckIn:IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var uid = context.Request.Params["uid"];
                var eventid = context.Request.Params["eventid"];
                int iuid = 0;
                int eid = 0;
                int.TryParse(uid, out iuid);
                int.TryParse(eventid, out eid);
                //int uid = Convert.ToInt32(context.Request.Params["uid"].ToString());
                //int eventid = Convert.ToInt32(context.Request.Params["eventid"].ToString());
                if (eid > 0 && iuid > 0)
                {
                    var memmodel = new MemberBLL(2052).getMemberByID((int)iuid);
                    var emodel = new EventsBLL(2052).selEvent((int)eid);
                    if (memmodel != null && emodel != null)
                    {
                        checkin(iuid, eid, context);
                    }
                    else
                    {
                        context.Response.Write(ResultUtility.ReturnResultJson("-600", "", "不存在该用户或活动"));
                    }
                }
                else
                {
                    context.Response.Write(ResultUtility.ReturnResultJson("-600", "", "不存在该用户或活动"));
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-600", "", "签到失败该用户ID或该活动Id不存在"));
            }
            
        }
        private void checkin(int uid,int eventid,HttpContext htc)
        {
            MemberCheckInDAL mca = new MemberCheckInDAL();
            if (mca.IsCheckIn(uid, eventid))
            {
                htc.Response.Write(ResultUtility.ReturnResultJson("-602", "", "已签到，不可以重复签到"));
            }
            else
            {
                if (mca.CheckIn(uid, eventid))
                {
                    UserEvent ue = new UserEvent();
                    ue.Uid = uid.ToString();
                    ue.Eventid = eventid.ToString();
                    string s = JsonSerializationHelper.ObjectToJson(ue);
                    htc.Response.Write(ResultUtility.ReturnResultJson("0", s, "签到成功"));
                }
                else
                {
                    htc.Response.Write(ResultUtility.ReturnResultJson("-601", "", "签到失败"));
                }
            }
        }
    }
}
